<?php
namespace Kxdrmobile\Model;
use Think\Model\ViewModel;
class UserMetalModel extends ViewModel{
	public $viewFields = array(
     'metal'=>array('_table'=>"sm_kxdr_honorary_medal",'id'=>"metal_id",'getway','score','sid','title','pic_url','cfg','version','status','_type'=>'LEFT'),
     'metal_user'=>array('_table'=>"sm_kxdr_honorary_medal_user",'_on'=>'metal_user.medal_id=metal.id','id'=>'muid','sid','user_id','medal_id','medal_num','get_time','raise_time','_type'=>'LEFT'),
     );
	public $sid;
	public $uid;
	public $mid;
	public $gift_id;
	public $limit;
	public $all_count;
	public $gropu_count;
	public function loadMyMetals(){
		$where["sid"]=$this->sid;
		$metal = $this->where($where)->order("count DESC,score ASC")->limit($this->limit)->getField("metal.id AS gift_id,medal_num AS count,pic_url AS gift_pic_url,title AS gift_name,get_time,getway,score");
		$USR = D("User");
		$USR->uid = $this->uid;
		$USR->sid = $this->sid;
		$USR->myfields = "`nengli`, `friends`, `joined_events`, `win_guesses`, `win_gifts`";
		$my_info = $USR->getUserFields();
		$my_info["jifen"] = $USR->getUserJifen();

		$top = Array();
		$bottom = Array();
		foreach($metal as $k=>$met){
			if($met["getway"]=="join_times" && $met["count"]<=0 && $met["score"]<=$my_info["joined_events"]){
				$metal[$k]["get"]=true;
			}
			if($met["getway"]=="exact_times"  && $met["count"]<=0 && $met["score"]<=$my_info["win_guesses"]){
				$metal[$k]["get"]=true;
			}
			if($met["getway"]=="power_score"  && $met["count"]<=0 && $met["score"]<=$my_info["nengli"]){
				$metal[$k]["get"]=true;
			}
			if($met["getway"]=="jifen"  && $met["count"]<=0 && $met["score"]<=$my_info["jifen"]){
				$metal[$k]["get"]=true;
			}
			if($met["getway"]=="gift_quant"  && $met["count"]<=0 && $met["score"]<=$my_info["win_gifts"]){
				$metal[$k]["get"]=true;
			}
			if($met["getway"]=="friend_quant"  && $met["count"]<=0 && $met["score"]<=$my_info["friends"]){
				$metal[$k]["get"]=true;
			}
			if($metal[$k]["get"]){
				$top[$k]=$metal[$k];
			}else{
				$bottom[$k]=$metal[$k];
			}
		}
		return array_merge($top,$bottom);
	}
	public function getAllCount(){
		$where["sid"]=$this->sid;
		// $where["user_id"]=$this->uid;
		$metal = $this->cache(true)->where($where)->limit($this->limit)->getField("count(metal.id)");
		return $metal;
	}
	public function picupMedal(){
		$where["sid"]=$this->sid;
		$where["metal_id"]=$this->mid;
		$metal = current($this->where($where)->getField("getway,medal_num AS count,score"));
		
		if($metal["count"]<=0){
			switch($metal["getway"]){
				case "join_times": $field = "joined_events"; break;
				case "exact_times": $field = "win_guesses"; break;
				case "power_score": $field = "nengli"; break;
				case "jifen": $field = "jifen"; break;
				case "gift_quant": $field = "win_gifts"; break;
				case "friend_quant": $field = "friends"; break;
			}
			
			$USR = D("User");
			$USR->uid = $this->uid;
			$USR->sid = $this->sid;
			
			if($field=="jifen"){
				$check = $USR->getUserJifen();
			}else{
				$USR->myfields = $field;
				$check = $USR->getUserField();
			}
			
			if($check >=$metal["score"]){
				// 验证通过允许领取
				$MET = M("HonoraryMedalUser");
				$met["sid"]=$this->sid;
				$met["user_id"]=$this->uid;
				$met["medal_id"]=$this->mid;
				$met["get_time"]=date("Y-m-d H:i:s");
				$met["medal_num"]=1;
				$MET->add($met);
				$re["msg"] = "成功领取！";
			}else{
				$re["msg"] = "抱歉，您还没有达到领取条件，不能领取这个奖章。";
				$re["check"] = $check;
				$re["sql"] = $USR->_sql();
			}
		}else{
			// 已经有这个奖章了
			$re["msg"]="已经领过了，不能重复领取";
		}
		return $re;
	}
}